package main

import (
	"fmt"
	"sync"
	"time"
)

var wg = sync.WaitGroup{}

func SetNum(chan1 chan int) {
	defer wg.Done()
	defer close(chan1)
	for i := 1; i <= 10; i++ {
		chan1 <- i
		time.Sleep(time.Second * 1)
	}
}

func Square(chan1 chan int, chan2 chan int) {
	defer wg.Done()
	defer close(chan2)
	for {
		x, ok := <-chan1
		if !ok {
			break
		}
		chan2 <- x * x
		time.Sleep(time.Second * 1)
	}
}

func GetNum(chan2 chan int) {
	defer wg.Done()
	for {
		x, ok := <-chan2
		if !ok {
			break
		}
		fmt.Printf("%v ", x)
		time.Sleep(time.Second * 1)
	}
}

func main() {
	st := int(time.Now().Unix())
	var chan1 chan int = make(chan int, 1)
	var chan2 chan int = make(chan int, 1)
	wg.Add(3)
	go SetNum(chan1)
	go Square(chan1, chan2)
	go GetNum(chan2)
	fmt.Printf("\n")
	wg.Wait()
	fmt.Println("use_time：", int(time.Now().Unix())-st)

}
